<?php
class Chart{

	public static function path($file){
		return '/assets/libs/flot/'.$file;
	}

	public static function base_js(){
		echo '<script type="text/javascript" src="'.self::path('jquery.flot.min.js').'"></script>';
		echo '<script type="text/javascript" src="/assets/javascripts/voca.chart.js"></script>';

	}

	public static function js($type,$include_base=TRUE){
		$type = strtolower($type);
		if($include_base){
			self::base_js();
		}
		if($type=='pie'){
			echo '<script type="text/javascript" src="'.self::path('jquery.flot.pie.min.js').'"></script>';
		}elseif ($type=='time') {
			echo '<script type="text/javascript" src="'.self::path('jquery.flot.time.min.js').'"></script>';
		}elseif ($type=='stack') {
			echo '<script type="text/javascript" src="'.self::path('jquery.flot.stack.min.js').'"></script>';
		}elseif ($type=='axislabels') {
			echo '<script type="text/javascript" src="'.self::path('jquery.flot.axislabels.js').'"></script>';
		}		
	}

	public static function data_familiarity_pie($user_id){
		$sql = 'SELECT familiarity, count(id) amount FROM `voca_user_words` WHERE user_id=? GROUP BY familiarity';
		$stmt = Repository::prepare($sql);
		$id=intval($user_id);
		$stmt -> bind_param('i',$id);
		$stmt -> execute();
		$rs = Repository::get_result($stmt);
		$result = array();
		if(count($rs)>0){
			foreach ($rs as $item) {
				$result[]=array(
					"label"=>familiarity($item['familiarity']).'('.$item['amount'].')', 
					"data"=>$item['amount'], 
					"color"=>familiarity_color($item['familiarity'])
				);
			}
		}
		return $result;
	}

	public static function data_words_increment($user_id){
		$sql = 'SELECT * FROM  `view_words_sum_per_day` WHERE user_id=? ORDER BY `date` DESC LIMIT 0,10';
		$stmt = Repository::prepare($sql);
		$id=intval($user_id);
		$stmt -> bind_param('i',$id);
		$stmt -> execute();
		$rs = Repository::get_result($stmt);
		$subsum = array();
		$sum=array();
		if(count($rs)>0){
			$i=0;
			for ($i=1; $i <=10 ; $i++) {
				$idx = 10-$i;
				if($idx<count($rs)){
					$item = $rs[$idx];
					$subsum[]=array($i, intval($item['amount']));
					$sum[]=array($i, intval($item['sum'])-intval($item['amount']));
				} else{
					$subsum[]=array($i, 0);
					$sum[]=array($i,0);
				}
			}
		}
		return array(
			"sum"=>array("label"=>"单词收藏总量","data"=>$sum,'color'=>'#F18D05'),
			"subsum"=>array("label"=>"当日单词收藏量","data"=>$subsum,'color'=>'#80B3FF')
		);
	}
}
?>